package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.AuthInfo;
import com.colorlight.cloud.complete.entity.po.SenderAuthorizationInfo;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 获取设备信息
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class GetDeviceAuthInfo implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        // 指令下发
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 设备授权信息
         */
        private AuthInfo authInfo;
        /**
         * 发送器授权信息列表
         */
        private List<SenderAuthorizationInfo> senderAuthorizationInfo;
    }

    private InputMsg input;
    private OutputMsg output;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.GetDeviceAuthInfo.InputMsg.Builder inputMsg =
                CompleteInstruction.GetDeviceAuthInfo.InputMsg.newBuilder();
        final CompleteInstruction.GetDeviceAuthInfo.Builder builder =
                CompleteInstruction.GetDeviceAuthInfo.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public GetDeviceAuthInfo byteConvertObject(ByteString bytes) {
        CompleteInstruction.GetDeviceAuthInfo getDeviceAuthInfo = null;
        try {
            getDeviceAuthInfo = CompleteInstruction.GetDeviceAuthInfo.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(getDeviceAuthInfo)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        GetDeviceAuthInfo.OutputMsg outputMsg = new GetDeviceAuthInfo.OutputMsg();
        CompleteInstruction.GetDeviceAuthInfo.OutputMsg output = getDeviceAuthInfo.getOutput();
        AuthInfo authInfo = new AuthInfo().formProto(output.getAuthInfo());
        List<SenderAuthorizationInfo> senderAuthorizationInfo = output.getSenderAuthorizationInfoList().stream()
                .map(proto -> new SenderAuthorizationInfo().formProto(proto))
                .collect(Collectors.toList());
        outputMsg.setAuthInfo(authInfo);
        outputMsg.setSenderAuthorizationInfo(senderAuthorizationInfo);
        this.setOutput(outputMsg);
        return this;
    }
}

